package com.kt.uokos.yimo.service.impl;

import com.github.pagehelper.PageHelper;
import com.kt.uokos.yimo.dao.Issue;
import com.kt.uokos.yimo.example.IssueExample;
import com.kt.uokos.yimo.mapper.IssueMapper;
import com.kt.uokos.yimo.service.IssueService;
import com.kt.uokos.yimo.util.ResponseUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class IssueServiceImpl implements IssueService {
    @Resource
    private IssueMapper issueMapper;
    @Override
    public List<Issue> querySelective(String question, Integer page, Integer size, String sort, String order) {
        IssueExample example = new IssueExample();
        IssueExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(question)){
        criteria.andQuestionLike("%"+question+"%");
        }
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            example.setOrderByClause(sort+" "+order);
        }
        PageHelper.startPage(page,size);
        return issueMapper.selectByExample(example);
    }

    @Override
    public void add(Issue issue) {
        issue.setAddTime(LocalDateTime.now());
        issue.setUpdateTime(LocalDateTime.now());
        issueMapper.insertSelective(issue);
    }

    @Override
    public void update(Issue issue) {
        issueMapper.updateByPrimaryKey(issue);
    }

    @Override
    public void delete(Issue issue) {
        issueMapper.deleteByPrimaryKey(issue.getId());
    }
}
